﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using System.Net.Http;
using System.Data;
using System.Data.SqlClient;


namespace Shop.Implementation.Customer
{
    public class CustomerHeadImage
    {
        #region 上传客户头像
        public static Dictionary<string,object> afterSave_CustomerHeadImg(MultipartFileData file)
        {
            NameValueCollection nv = System.Web.HttpContext.Current.Request.QueryString;
            string token = nv["token"].GetVal_String();
            string filename = file.Headers.ContentDisposition.FileName;
            string filelocal = file.LocalFileName;
            filename = filename.Replace("\"", "");
            string extense = filename.Substring(filename.LastIndexOf("."), filename.Length - filename.LastIndexOf("."));
            int idCus;
            bool bret = CustomerCommon.check_token(token, out idCus);
            if (!bret)
            {
                return CustomerCommon.NotLogin();
            }
            string cmdsql = "select cMobile from customer where id=" + idCus.ToString();
            string cusmobile = Common.HandleSQL.getObj(cmdsql).GetVal_String();
            string root = System.Web.HttpContext.Current.Server.MapPath("\\image\\customerhead");

            string pathtotal = root + "\\" + cusmobile + extense;
            if (System.IO.File.Exists(pathtotal))
            { System.IO.File.Delete(pathtotal); }

            System.IO.FileInfo finfo = new System.IO.FileInfo(filelocal);
            finfo.MoveTo(pathtotal);

            cmdsql = string.Format("update customer set cImgName=@cFileName where id={0}", idCus);
            using (SqlConnection cn = new SqlConnection(Common.HandleSQL.cConnstr))
            {
                try
                {
                    cn.Open();
                    SqlCommand cncmd = new SqlCommand(cmdsql, cn);
                    cncmd.Parameters.Add(new SqlParameter("@cFileName", "/image/customerhead/" + cusmobile + extense));
                    cncmd.ExecuteNonQuery();
                }
                finally
                { cn.Close(); cn.Dispose(); }
            }
            return Common.ReturnValue.Get("成功", true);
        }
        #endregion
    }
}